package com.bw.paper.mvp.view.Activity;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bw.paper.App;
import com.bw.paper.R;
import com.bw.paper.entity.ChannelEntity;
import com.bw.paper.entity.LocalChannelEntity;
import com.bw.paper.entity.MoreChannelEntity;
import com.bw.paper.entity.UserEntity;
import com.bw.paper.mvp.base.model.BaseEntity;
import com.bw.paper.mvp.base.view.BaseActivity;
import com.bw.paper.mvp.contact.LREContract;
import com.bw.paper.mvp.di.DaggerLREComponent;
import com.bw.paper.mvp.di.LREModules;
import com.bw.paper.mvp.model.HttpCode;
import com.bw.paper.mvp.presenter.LREPresenter;
import com.bw.paper.mvp.view.Fragment.news.Adapter.BaseUserChannelAdapter;
import com.bw.paper.mvp.view.Fragment.news.Adapter.LocalChannelAdapter;
import com.bw.paper.mvp.view.Fragment.news.Adapter.MoreChannelAdapter;
import com.bw.paper.mvp.view.Fragment.news.Adapter.UserChannelAdapter;
import com.bw.paper.widget.ChannelItemDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.cyan.dragrecyclerview.DragRecyclerView;
import cn.cyan.dragrecyclerview.HoldTouchHelper;

public class ChannelActivity extends BaseActivity<LREPresenter> implements
        LREContract.ILREView, HoldTouchHelper.OnItemTouchEvent,
        View.OnClickListener, OnItemClickListener {

    private ImageView channel_back_img;
    private DragRecyclerView channel_dragrv;
    private View view_channel_head;
    private View view_channel_local;
    private View view_channel_more;
    private View view_channel_user;
    private View view_channel_foot_local;
    private View view_channel_foot_more;
    private RecyclerView channel_foot_local_rv;
    private RecyclerView channel_more_rv;
    private TextView channel_user_edt_tv;
    private UserChannelAdapter channelAdapter;
    private LocalChannelAdapter localChannelAdapter;
    private MoreChannelAdapter moreChannelAdapter;
    private boolean edtFlag = false;//false状态时频道不可编辑，true可编辑

    @Override
    public void initView() {
        channel_back_img = f(R.id.channel_back_img);
        channel_dragrv = f(R.id.channel_dragrv);
        view_channel_head = LayoutInflater.from(this).inflate(R.layout.view_channel_head,null);
        view_channel_user = LayoutInflater.from(this).inflate(R.layout.view_channel_user,null);
        view_channel_local = LayoutInflater.from(this).inflate(R.layout.view_channel_local,null);
        view_channel_more = LayoutInflater.from(this).inflate(R.layout.view_channel_more,null);
        view_channel_foot_local = LayoutInflater.from(this).inflate(R.layout.view_channel_foot_local,null);
        view_channel_foot_more = LayoutInflater.from(this).inflate(R.layout.view_channel_foot_more,null);
        channel_user_edt_tv = view_channel_user.findViewById(R.id.channel_user_edt_tv);
        channel_foot_local_rv = view_channel_foot_local.findViewById(R.id.channel_foot_local_rv);
        channel_more_rv = view_channel_foot_more.findViewById(R.id.channel_more_rv);
        GridLayoutManager userManager = new GridLayoutManager(this,4);
        userManager.setOrientation(GridLayoutManager.VERTICAL);
        GridLayoutManager moreManager = new GridLayoutManager(this,4);
        moreManager.setOrientation(GridLayoutManager.VERTICAL);
        GridLayoutManager lcoalManager = new GridLayoutManager(this,4);
        lcoalManager.setOrientation(GridLayoutManager.VERTICAL);
        channel_foot_local_rv.setLayoutManager(lcoalManager);
        channel_more_rv.setLayoutManager(moreManager);
        channel_dragrv.setLayoutManager(userManager);
        channel_more_rv.addItemDecoration(new ChannelItemDecoration(this));
        channel_dragrv.addItemDecoration(new ChannelItemDecoration(this));
        channel_foot_local_rv.addItemDecoration(new ChannelItemDecoration(this));
        localChannelAdapter = new LocalChannelAdapter();
        moreChannelAdapter = new MoreChannelAdapter();
        moreChannelAdapter.setOnItemClickListener(this);
        localChannelAdapter.setOnItemClickListener(this);
        channel_foot_local_rv.setAdapter(localChannelAdapter);
        channel_more_rv.setAdapter(moreChannelAdapter);
        channelAdapter = new UserChannelAdapter();
        channelAdapter.addHeaderView(view_channel_head,0);
        channelAdapter.addHeaderView(view_channel_user,1);
        channelAdapter.addFooterView(view_channel_more);
        channelAdapter.addFooterView(view_channel_foot_more);
        channelAdapter.addFooterView(view_channel_local);
        channelAdapter.addFooterView(view_channel_foot_local);
        channel_dragrv.setDragAdapter(channelAdapter)
                .dragEnable(true)
                .showDragAnimation(true)
                .bindEvent(this);
        channel_back_img.setOnClickListener(this::onClick);
        channel_user_edt_tv.setOnClickListener(this::onClick);
    }

    @Override
    public View addStatusView() {
        return f(R.id.channel_root);
    }

    @Override
    public int bindlayout() {
        return R.layout.activity_channel;
    }

    @Override
    public void initData() {
        List<UserEntity>list = App.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();
        long user_id = 0;
        if (list.size()>0)
            user_id = list.get(0).user_id;
        //我的频道
        Map<String,Object>userMap = new HashMap<>();
        userMap.put("code", HttpCode.USERCHANNELCODE);
        userMap.put("imie","");
        userMap.put("userId",user_id);
        //更多频道
        Map<String,Object>moreMap = new HashMap<>();
        moreMap.put("code", HttpCode.MORECHANNELCODE);
        moreMap.put("imie","");
        moreMap.put("userId",user_id);
        //地方频道
        Map<String,Object>localMap = new HashMap<>();
        localMap.put("code", HttpCode.LOCALCHANNELCODE);
        localMap.put("imie","");
        localMap.put("userId",user_id);
        p.requestAll(localMap,moreMap,userMap);
    }

    @Override
    public void inject() {
        DaggerLREComponent.builder().lREModules(
                new LREModules(this)).build().injectChannelActivity(this);
    }

    @Override
    public void refreshAll(BaseEntity entity) {

        if (entity instanceof ChannelEntity){
            ChannelEntity channelEntity = (ChannelEntity) entity;
            //我的频道接口返回值
            channelAdapter.setNewInstance(channelEntity.getValues());
        }else if (entity instanceof LocalChannelEntity){
            LocalChannelEntity localChannel = (LocalChannelEntity) entity;
            //本地频道接口返回值
            localChannelAdapter.setNewInstance(localChannel.getValues());
        }else if (entity instanceof MoreChannelEntity){
            //更多频道接口返回值
            MoreChannelEntity moreChannel = (MoreChannelEntity) entity;
            moreChannelAdapter.setNewInstance(moreChannel.getValues());
        }else {
            //收藏频道接口返回值
            if (entity.status.equals("200")){
                //保存界面数据成功
                //通知newsFragment更新频道数据

            }
        }

    }

    @Override
    public void refreshRecyckerView(BaseEntity entity) {

    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {

    }

    @Override
    public void onLongPress(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int position) {
        if (!edtFlag){
            return;
        }
        if (((BaseUserChannelAdapter) recyclerView.getAdapter()).onItemDrag(position)) {
            ((DragRecyclerView) recyclerView).startDrag(position);
        }
    }

    @Override
    public void onItemClick(RecyclerView recyclerView,
                            RecyclerView.ViewHolder viewHolder,
                            int position) {

    }




    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.channel_back_img == id){
            finish();
        }else if(R.id.channel_user_edt_tv == id){
            String str = channel_user_edt_tv.getText().toString();
            if (str.equals("编辑")){
                channel_user_edt_tv.setText("完成");
                //我的频道可拖动,地方以及更多频道可通过item点击添加到我的频道中
                edtFlag = true;
            }else {
                channel_user_edt_tv.setText("编辑");
                edtFlag = false;
                //发起网络请求保存用户最新收藏的频道
                updateChannel();
            }
        }
    }

    private void updateChannel(){
        List<UserEntity>list = App.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();
        long user_id = 0;
        if (list.size()>0)
            user_id = list.get(0).user_id;
        List<Map<String,Object>>channels = new ArrayList<>();
        List<ChannelEntity.Values>datas = channelAdapter.getData();
        for (int i = 0;i < datas.size();i ++){
            ChannelEntity.Values values = datas.get(i);
            Map<String,Object>map = new HashMap<>();
            map.put("channel_id",values.channel_id);
            channels.add(map);
        }
        Map<String,Object>map = new HashMap<>();
        map.put("code",HttpCode.COLLECTIONCHANNELCODE);
        map.put("imie","");
        map.put("user_id",user_id);
        map.put("channeles",channels);
        p.requestAll(map);
    }

    @Override
    public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
        if (!edtFlag){
            return;
        }
        //区分对应哪个Adapter被点击local more
        if (adapter instanceof LocalChannelAdapter){
            //本地
            LocalChannelEntity.Values entity = localChannelAdapter.getItem(position);
            ChannelEntity.Values values = new ChannelEntity.Values();
            values.dropEnable = true;
            values.dragEnable = true;
            values.channel_hot = entity.channel_hot;
            values.channel_id = entity.channel_id;
            values.channel_name = entity.channel_name;
            channelAdapter.addData(values);
            localChannelAdapter.remove(entity);

        }else {
            //更多
            MoreChannelEntity.Values entity = moreChannelAdapter.getItem(position);
            ChannelEntity.Values values = new ChannelEntity.Values();
            values.dropEnable = true;
            values.dragEnable = true;
            values.channel_hot = entity.channel_hot;
            values.channel_id = entity.channel_id;
            values.channel_name = entity.channel_name;
            channelAdapter.addData(values);
            moreChannelAdapter.remove(entity);
        }
    }
}